package com.zou.baidu_music_radio.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.zou.baidu_music_radio.Constants;
import com.zou.baidu_music_radio.PlayingActivity;
import com.zou.baidu_music_radio.R;
import com.zou.baidu_music_radio.bean.Hotsongsbean;
import com.zou.baidu_music_radio.utils.ImageUtil;

import java.util.List;

/**
 * Created by zou on 2016/3/2.
 */
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {
    List<Hotsongsbean.SongListEntity> datas;
    Context                           context;

    public MyAdapter(List datas, Context context) {
        this.datas = datas;
        this.context = context;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        MyViewHolder holder = new MyViewHolder(LayoutInflater.from(context).inflate(R.layout.item_song, parent, false));
        return holder;
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        holder.songName.setText(datas.get(position).getTitle());
        holder.artistName.setText(datas.get(position).getArtist_name());
        ImageUtil.load(context, holder.imageView, datas.get(position).getPic_big());

    }


    @Override
    public int getItemCount() {
        if (datas == null)
            return 0;
        return datas.size();
    }


    class MyViewHolder extends RecyclerView.ViewHolder {
        TextView  songName;
        TextView  artistName;
        ImageView imageView;

        public MyViewHolder(View itemView) {
            super(itemView);
            songName = (TextView) itemView.findViewById(R.id.songNameListView);
            artistName = (TextView) itemView.findViewById(R.id.artistNameSongListView);
            imageView = (ImageView) itemView.findViewById(R.id.songsAlbum);
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(context, PlayingActivity.class);
                    intent.putExtra(Constants.SONGID, datas.get(getAdapterPosition()).getSong_id() + datas.get(getAdapterPosition()).getArtist_name());
                    context.startActivity(intent);
                }
            });
        }
    }
}
